package com.example.demo.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LIN
 * @date 2022-10-03 21:24
 */
public class VerySimpleLeaveProcessTest {

    public static void main(String[] args) throws Exception {
        new VerySimpleLeaveProcessTest().testStartProcess2();
    }
    public void testStartProcess() throws Exception {
        // 创建流程引擎，使用内存数据库
        ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration().buildProcessEngine();
        // 部署流程定义文件
        RepositoryService repositoryService = processEngine.getRepositoryService();

        repositoryService.createDeployment().addClasspathResource("activiti/leave.bpmn").deploy();
        // 验证已部署流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
        String key = processDefinition.getKey();
        System.out.println(key);

        // 启动流程并返回流程实例
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);

        String id = processInstance.getId();
        String processDefinitionId = processInstance.getProcessDefinitionId();
        System.out.println("pid="+id+" ,pdid="+processDefinitionId);
    }


    public void testStartProcess2(){
        // 创建流程引擎，使用内存数据库
        ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration().buildProcessEngine();
        // 流程引擎的名称默认为default
        System.out.println(processEngine.getName());
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String bpmnFileName = "activiti/SayHelloToLeave.bpmn";
        // 加载资源
        Deployment deploy = repositoryService.createDeployment().addInputStream("SayHelloToLeave.bpmn", this.getClass().getClassLoader().getResourceAsStream(bpmnFileName)).deploy();

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
        System.out.println(processDefinition.getKey());

        RuntimeService runtimeService = processEngine.getRuntimeService();



        Map<String, Object> variables = new HashMap<>(16);
//      申请人的名称
        variables.put("applyUser","employee1");
        variables.put("user","employee2");
//        请假的天数
        variables.put("days",3);
//      启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("SayHelloToLeave", variables);

        System.out.println("pid="+processInstance.getId()+",pdid="+processInstance.getProcessDefinitionId());

        TaskService taskService = processEngine.getTaskService();

//        查询组（Group）deptLeader的未签收任务并验证任务的名称
        Task deptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
        System.out.println("领导审批，"+deptLeader.getName());
//        调用claim方法签收此任务归用户leaderUser所有
        taskService.claim(deptLeader.getId(),"leaderUser");

        variables = new HashMap<String,Object>(16);
//      设置变量approval为true表示审批通过
        variables.put("approved",true);
        taskService.complete(deptLeader.getId(),variables);

        deptLeader = taskService.createTaskQuery().taskCandidateGroup("deptLeader").singleResult();
        System.out.println(deptLeader==null);
        HistoryService historyService = processEngine.getHistoryService();
//        通过历史接口统计已经完成finished的流程实例数量
        long count = historyService.createHistoricActivityInstanceQuery().finished().count();
        System.out.println(count);


    }
}
